﻿Public Class STOCK
    Dim PROVIDER_Check As New PROVIDERSCLASS
    Dim connect As CONDB = CONDB.NewConnection()
    Dim dtsetStock As DataSet
    Dim s2 As FILTERCLASS

    Public Sub queryStock()
        dtsetStock = New DTSETSTOCK

        Dim objGetStock As New STOCKCLASS
        Dim sql As String = objGetStock.selectStock_
        connect.GetTable(sql, dtsetStock.Tables("stock"))

        connect.Dispose()

        xgvStock.DataSource = dtsetStock
        xgvStock.ForceInitialize()
    End Sub

    Private Sub SROOMITEM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        queryStock()

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        txtStkid.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("stkid").ToString()
        txtStkcode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("stkcode").ToString()
        txtStkname.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("stkname").ToString()
        txtLocation.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("location").ToString()

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_sale") IsNot DBNull.Value Then
            ckbF_sale.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_sale"))
        Else
            ckbF_sale.Checked = False
        End If

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_drug") IsNot DBNull.Value Then
            ckbF_drug.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_drug"))
        Else
            ckbF_drug.Checked = False
        End If

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_medsup") IsNot DBNull.Value Then
            ckbF_medsup.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_medsup"))
        Else
            ckbF_medsup.Checked = False
        End If

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_supply") IsNot DBNull.Value Then
            ckbF_supply.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("f_supply"))
        Else
            ckbF_supply.Checked = False
        End If

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("status") IsNot DBNull.Value Then
            ckbStatus.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("status"))
        Else
            ckbStatus.Checked = False
        End If
    End Sub

    Private Sub btnAdddata_Click(sender As Object, e As EventArgs) Handles btnAdddata.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If txtStkcode.Text.Length.ToString > 0 And txtStkname.Text.Length.ToString > 0 Then

                    Dim objAddStock As New STOCKCLASS
                    objAddStock.STKCODE_ = txtStkcode.Text.Trim
                    objAddStock.STKNAME_ = txtStkname.Text.Trim
                    objAddStock.LOCATION_ = txtLocation.Text.Trim
                    objAddStock.F_SALE_ = ckbF_sale.Checked
                    objAddStock.F_DRUG_ = ckbF_drug.Checked
                    objAddStock.F_MEDSUP_ = ckbF_medsup.Checked
                    objAddStock.F_SUPPLY_ = ckbF_supply.Checked
                    objAddStock.STATUS_ = ckbStatus.Checked

                    If objAddStock.insertStock = True Then
                        queryStock()
                        clearForm()
                    End If
                Else
                    MsgBox("ท่านต้องระบุ รหัส stock และชื่อ stock")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub btnEditData_Click(sender As Object, e As EventArgs) Handles btnEditData.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูล stock ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    If txtStkcode.Text.Length.ToString > 0 And txtStkname.Text.Length.ToString > 0 And txtStkid.Text.Length.ToString > 0 Then

                        Dim objEditStock As New STOCKCLASS
                        objEditStock.STKID_ = txtStkid.Text.Trim
                        objEditStock.STKCODE_ = txtStkcode.Text.Trim
                        objEditStock.STKNAME_ = txtStkname.Text.Trim
                        objEditStock.LOCATION_ = txtLocation.Text.Trim
                        objEditStock.F_SALE_ = ckbF_sale.Checked
                        objEditStock.F_DRUG_ = ckbF_drug.Checked
                        objEditStock.F_MEDSUP_ = ckbF_medsup.Checked
                        objEditStock.F_SUPPLY_ = ckbF_supply.Checked
                        objEditStock.STATUS_ = ckbStatus.Checked

                        If objEditStock.editStock = True Then
                            queryStock()
                            clearForm()
                        End If
                    Else
                        MsgBox("ท่านต้องระบุ รหัส stock และชื่อ stock")
                    End If
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("sroomitem")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("sroomitem")
        nextform.ShowDialog()
    End Sub

    Private Sub clearForm()
        txtStkid.Clear()
        txtStkcode.Clear()
        txtStkname.Clear()
        txtLocation.Clear()
        ckbF_sale.Checked = False
        ckbF_drug.Checked = False
        ckbF_medsup.Checked = False
        ckbF_supply.Checked = False
        ckbStatus.Checked = False
    End Sub
End Class